﻿#include "u_syslog_page.h"

SysLogPage::SysLogPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
    this->UpdateTable();

    this->setTitle("日志记录");
    this->resize(1100, 600);
}

void SysLogPage::InitWidget(){
    btnQuery  = new QPushButton(QChar(0xe639) + QString(" 查询"));
    btnDel  = new QPushButton(QChar(0xe603) + QString(" 删除"));
    btnExport   = new QPushButton(QChar(0xe87f) + QString(" 导出"));

    editCreateTime = new QDateTimeEdit();
    editCreateTime->setDisplayFormat("yyyy-MM-dd");
    editCreateTime->setCalendarPopup(true);
    editCreateTime->setDateTime(QDateTime::currentDateTime());

    editType = new QComboBox(); editType->addItems({"全部", "系统日志", "操作日志"});
    editDescribe = new QLineEdit();

    QStringList headers = QString("ID,类型,用户,日志描述,日志时间").split(",");
    tableWidget = MyGUI::GetTableWidget(headers, 150, false, false);
    tableWidget->setColumnHidden(0, true);//隐藏

    //---------------------------------------------------------------
    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->addWidget(new QLabel("日志时间"));
    layout1->addWidget(editCreateTime);
    layout1->addWidget(new QLabel("日志类型"));
    layout1->addWidget(editType);
    layout1->addWidget(new QLabel("关键字"));
    layout1->addWidget(editDescribe);
    layout1->addStretch();
    layout1->addWidget(btnQuery);
    //layout1->addWidget(btnDel);
    //layout1->addWidget(btnExport);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(layout1);

    //---------------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(groupbox11);
    mainLayout->addWidget(tableWidget);
    this->addLayout(mainLayout);
}

void SysLogPage::SetupConnections(){
    connect(btnQuery , SIGNAL(clicked()), this, SLOT(OnBtnQueryClicked()));
    connect(btnDel , SIGNAL(clicked()), this, SLOT(OnBtnDelClicked()));
    connect(btnExport , SIGNAL(clicked()), this, SLOT(OnBtnExportClicked()));
}

void SysLogPage::Initialize(){
    SysLog query;
    query.createTime = editCreateTime->dateTime();
    query.type = editType->currentIndex() - 1;
    query.describe = editDescribe->text();

    queryList = globalData->sqlBase->GetSysLogList(query);
}

void SysLogPage::UpdateTable(){
    int count = queryList.size();

    tableWidget->setRowCount(0);
    tableWidget->setRowCount(count);

    for(int i=0; i<count; i++){
        SysLog info = queryList.at(i);

        tableWidget->setItem(i, 0, MyGUI::GetTableWidgetItem(info.id));
        tableWidget->setItem(i, 1, MyGUI::GetTableWidgetItem(info.GetTypeText()));
        tableWidget->setItem(i, 2, MyGUI::GetTableWidgetItem(info.operater));
        tableWidget->setItem(i, 3, MyGUI::GetTableWidgetItem(info.describe, Qt::AlignLeft | Qt::AlignVCenter));
        tableWidget->setItem(i, 4, MyGUI::GetTableWidgetItem(info.createTime));
    }
}

void SysLogPage::OnBtnQueryClicked(){
    this->Initialize();
    this->UpdateTable();
}

void SysLogPage::OnBtnDelClicked(){
    if(!MyGUI::ShowMessageBoxQuesion("确定要删除选中的记录？", this)){
        return;
    }

    QMap<int, int> idMap;
    QList<QTableWidgetItem*>items = tableWidget->selectedItems();
    int count = items.count();
    for(int i=0;i<count;i++) {
        int row = tableWidget->row(items.at(i));//获取选中的行
        QTableWidgetItem *item = tableWidget->item(row,0);
        idMap.insert(item->text().toInt(), row);
    }

    bool b = true;
    QMap<int, int>::iterator it;
    for (it = idMap.begin(); it != idMap.end(); ++it) {
        b = b & globalData->sqlBase->DelSysLog(it.key());
    }

    if(b){
        MyGUI::ShowMessageBoxInfo("删除成功", this);
    }else{
        MyGUI::ShowMessageBoxError("删除失败", this);
    }

    this->Initialize();
    this->UpdateTable();
}

void SysLogPage::OnBtnExportClicked(){
    QString fileName = QString("日志记录-%1.xlsx").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    QString filePath = QFileDialog::getSaveFileName(this, "保存", fileName, "*.xls;*.xlsx;");

    if(filePath.isEmpty()){
        return;
    }
}
